package routers

import (
	"cybeego/controllers"

	"github.com/astaxie/beego/context"

	"github.com/astaxie/beego"
)

func init() {
	// 设置路由过滤，使用正则匹配，在路由之前，判断session，否则重定向
	beego.InsertFilter("/User/*", beego.BeforeRouter, filterFunc)
	beego.InsertFilter("/Domain/*", beego.BeforeRouter, filterFunc)
	beego.InsertFilter("/Task/*", beego.BeforeRouter, filterFunc)
	// 注册登录路由
	beego.Router("/register", &controllers.RegController{}, "get:ShowReg;post:HandleReg")
	beego.Router("/", &controllers.LoginController{}, "get:ShowLogin;post:HandleLogin")

	//退出路径
	beego.Router("/logout", &controllers.LogoutController{}, "get:HandleLogout")
	// 用户路由
	beego.Router("/User/ShowUser", &controllers.UserController{}, "get:ShowUserList")
	beego.Router("/User/AddUser", &controllers.UserController{}, "get:AddUserList;post:AddUser")
	// 域名路由
	beego.Router("/Domain/ActivateDomain", &controllers.DomainController{}, "*:ActivateDomain")
	beego.Router("/Domain/ShowDomain", &controllers.DomainController{}, "get:ShowDomainList")
	beego.Router("/Domain/AddDomain", &controllers.DomainController{}, "get:ShowAddDomain;post:AddDomain")
	beego.Router("/Domain/details", &controllers.DomainController{}, "get:ShowDomainDetails")
	beego.Router("/Domain/DeleteDomain", &controllers.DomainController{}, "get:DeleteDomain")
	beego.Router("/Domain/UpdateDomain", &controllers.DomainController{}, "get:ShowUpdateDomain;post:UpdateDomain")
	beego.Router("/Domain/Download", &controllers.DomainController{}, "get:DownloadDomain")

	// DomainType 路由
	beego.Router("/Domain/AddDomainType", &controllers.DomainTypeController{}, "get:ShowDomainType;post:AddDomainType")
	beego.Router("/Domain/DeleteDomainType", &controllers.DomainTypeController{}, "*:DeleteDomainType")
	//
	// 任务路由
	beego.Router("/Task/ShowTask", &controllers.TaskController{}, "get:ShowAddTask")
	beego.Router("/Task/AddTask", &controllers.TaskController{}, "post:AddTask")
	beego.Router("/Task/DeleteTask", &controllers.TaskController{}, "*:DeleteTask")
	// 测试路由
	beego.Router("/file", &controllers.UploadController{}, "*:UpForm")
	beego.Router("/upload", &controllers.UploadController{}, "*:UpFile")
}

//检查session，此函数由正则匹配的路由，在寻找路由之前执行
func filterFunc(ctx *context.Context) {
	if name := ctx.Input.Session("username"); name == nil {
		ctx.Redirect(302, "/")
		return
	}

}
